package com.quasar.hpatient.module.doctor_chat;

import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;
import android.widget.TextView;

import com.quasar.hpatient.R;
import com.quasar.hpatient.bean.comm_album.AlbumPhotoBean;
import com.quasar.hpatient.module.comm_album.AlbumActivity;
import com.quasar.hpatient.module.comm_camera.CameraActivity;

import java.util.ArrayList;
import java.util.List;

import lib.quasar.base.frame.BaseActivity;
import lib.quasar.db.manager.DBManager;
import lib.quasar.widget.emoji.EmojiLayout;
import lib.quasar.widget.photo.PhotoLayout;
import lib.quasar.widget.photo.PhotoModel;

/**
 * description: 聊天
 * created by kalu on 2018/6/11 22:59
 */
public final class DoctorChatActivity extends BaseActivity<DoctorChatPresenter> implements DoctorChatView {

    public final static int REQUEST_CODE = 1001;
    public final static String CHAT_ID = "CHAT_ID";
    public final static String CHAT_NAME = "CHAT_NAME";
    private String doctorId = "";

    @Override
    public int initView() {
        return R.layout.activity_patient_chat;
    }

    @Override
    public void initDataLocal() {

        doctorId = getIntent().getStringExtra(CHAT_ID);
        final String doctorName = getIntent().getStringExtra(CHAT_NAME);

        if (!"".equals(doctorId))
            DBManager.getInstance().clearMessage(Long.parseLong(doctorId));

        final TextView chatName = findViewById(R.id.doctor_chat_name);
        setText(chatName, doctorName);

        final RecyclerView chatRecycler = findViewById(R.id.doctor_chat_list);
        final EmojiLayout chatEmoji = findViewById(R.id.doctor_chat_emoji);

        getPresenter().loadMessage(this, doctorId);
        getPresenter().initList(this, chatRecycler, chatEmoji);
        getPresenter().initBroadcast(this, chatRecycler, doctorId);

        final EmojiLayout emoji = findViewById(R.id.doctor_chat_emoji);
        emoji.setOnInputChangeListener(new EmojiLayout.OnInputChangeListener() {

            @Override
            public void onMenuSend(String str) {
                getPresenter().sendMessage(DoctorChatActivity.this, chatRecycler, str, "message", doctorId, doctorName);
            }

            @Override
            public void onMenuLayout(RecyclerView recycler) {
                getPresenter().initMenuAdapter(DoctorChatActivity.this, recycler);
            }

            @Override
            public void onMenuSwitch(RecyclerView recycler) {
            }
        });
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (!"".equals(doctorId))
            DBManager.getInstance().clearMessage(Long.parseLong(doctorId));
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        switch (resultCode) {
            case AlbumActivity.RESULT_CODE:
                if (null != data) {
                    final String doctorId = getIntent().getStringExtra(CHAT_ID);
                    final String doctorName = getIntent().getStringExtra(CHAT_NAME);
                    final ArrayList<AlbumPhotoBean> list = data.getParcelableArrayListExtra(AlbumActivity.LIST);
                    final RecyclerView mRecycler = findViewById(R.id.doctor_chat_list);
                    getPresenter().compressImage(this, mRecycler, list, doctorId, doctorName);
                }
                break;

            case AlbumActivity.RESULT_CODE_OPEN_CAMERA:
                final Intent intent = new Intent(getApplicationContext(), CameraActivity.class);
                startActivityForResult(intent, CameraActivity.RESULT_CODE);
                break;

            case CameraActivity.RESULT_CODE:
                if (null == data) break;
                final String doctorId = getIntent().getStringExtra(CHAT_ID);
                final String doctorName = getIntent().getStringExtra(CHAT_NAME);
                final String path = data.getStringExtra(CameraActivity.PATH);
                final RecyclerView mRecycler = findViewById(R.id.doctor_chat_list);
                break;
        }
    }

    @Override
    public void showPhoto(int position, List<? extends PhotoModel> littleList, List<? extends PhotoModel> bigList, List<ImageView> imageViews) {
        if (littleList.isEmpty() || bigList.isEmpty() || imageViews.isEmpty())
            return;

        new PhotoLayout.Builder(this)
                .setPhotoDefaultPosition(position)
                .setPhotoLongPressSave(false)
                .setPhotoOpenTransAnim(true)
                .setPhotoBackgroundColor(Color.BLACK)
                .setPhotoDefaultResource(R.mipmap.ic_launcher)
                .setPhotoViewList(imageViews)
                .setPhotoUrlList(bigList)
                .setPhotoLittleUrlList(littleList)
                .show();
    }
}
